home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15234 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this a memory leak?
  5. Date: 4 Apr 1996 21:33:38 +1000
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4k0c2i$h6e@werple.net.au>
  8. References: <4jv214$gv7@insosf1.netins.net>
  9. NNTP-Posting-Host: werplez.mira.net.au
  10.  
  11. hhowe@trgnet.com (Harold Howe) writes:
  12.  
  13. >Could someone please tell me if this code leaks memory
  14.  
  15. >class BuriedClass
  16. >  {
  17. >  ...
  18. >  }
  19.  
  20. >class TopClass
  21. >  {
  22. >  private
  23. >    BuriedClass *bury;
  24. >  public:
  25. >    TopClass::TopClass() { bury = new BuriedClass();}
  26. >    shutDown             { bury = 0;}
  27. >    ~TopClass            { delete bury}
  28. >  }
  29.  
  30. >int main(void)
  31. >  {
  32. >  TopClass *top = new TopClass();
  33. >  top->shutDown();
  34. >  delete top;
  35. >  return 0;
  36. >  }
  37.  
  38. >Is the memory that was alloced for the buried class lost? If not please 
  39. >describe how it was freed.  Does zeroing the pointer free the memory? The 
  40. >delete is a waste of time on a zeroed pointer, isn't it.
  41.  
  42. The BuriedClass object is not deleted. Zeroing the pointer does nothing
  43. but zero the pointer, which prevents the BuriedClass object from being
  44. deleted. Maybe 'shutDown' is intended to be called if responsibility for
  45. the BuriedClass object is moved to something else, which will delete it,
  46. or maybe it's a safety measure, to be used if some sort of free store
  47. corruption is detected. 
  48.  
  49. David White
  50. davidw@werple.mira.net.au
  51.